home *** CD-ROM | disk | FTP | other *** search
/ CD BIT 75 / CD BIT 75.iso / Software / mysql-4.0.22-win / data1.cab / Development / examples / tests / fork_big.pl < prev    next >
Encoding:
Perl Script  |  2004-10-28  |  14.0 KB  |  580 lines

  1. #!/usr/bin/perl -w
  2. #
  3. # This is a test with uses many processes to test a MySQL server.
  4. #
  5. # Tested a lot with:  --threads=30
  6.  
  7. $opt_loop_count=500000; # Change this to make test harder/easier
  8.  
  9. ##################### Standard benchmark inits ##############################
  10.  
  11. use DBI;
  12. use Getopt::Long;
  13. use Benchmark;
  14.  
  15. package main;
  16.  
  17. $opt_skip_create=$opt_skip_in=$opt_verbose=$opt_fast_insert=
  18. $opt_lock_tables=$opt_debug=$opt_skip_delete=$opt_fast=$opt_force=0;
  19. $opt_threads=5;
  20. $opt_host=$opt_user=$opt_password=""; $opt_db="test";
  21.  
  22. GetOptions("host=s","db=s","user=s","password=s","loop-count=i","skip-create","skip-in","skip-delete","verbose","fast-insert","lock-tables","debug","fast","force","threads=i") || die "Aborted";
  23. $opt_verbose=$opt_debug=$opt_lock_tables=$opt_fast_insert=$opt_fast=$opt_skip_in=$opt_force=undef;  # Ignore warnings from these
  24.  
  25. print "Test of multiple connections that test the following things:\n";
  26. print "insert, select, delete, update, alter, check, repair and flush\n";
  27.  
  28. @testtables = ( ["bench_f31", ""],
  29.         ["bench_f32", "row_format=fixed"],
  30.         ["bench_f33", "delay_key_write=1"],
  31.         ["bench_f34", "checksum=1"],
  32.         ["bench_f35", "delay_key_write=1"]);
  33. $abort_table="bench_f39";
  34.  
  35. $numtables = $#testtables+1;
  36. srand 100;            # Make random numbers repeatable
  37.  
  38. ####
  39. ####  Start timeing and start test
  40. ####
  41.  
  42. $start_time=new Benchmark;
  43. $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  44.             $opt_user, $opt_password,
  45.           { PrintError => 0}) || die $DBI::errstr;
  46. if (!$opt_skip_create)
  47. {
  48.   my $table_def;
  49.   foreach $table_def (@testtables)
  50.   {
  51.     my ($table,$extra)= ($table_def->[0], $table_def->[1]);
  52.     print "Creating table $table in database $opt_db\n";
  53.     $dbh->do("drop table if exists $table");
  54.     $dbh->do("create table $table".
  55.          " (id int(6) not null auto_increment,".
  56.          " info varchar(32)," .
  57.          " marker timestamp," .
  58.          " flag int not null," .
  59.          " primary key(id)) $extra")
  60.  
  61.       or die $DBI::errstr;
  62.     # One row in the table will make future tests easier
  63.     $dbh->do("insert into $table (id) values (null)")
  64.       or die $DBI::errstr;
  65.   }
  66.   # Create the table we use to signal that we should end the test
  67.   $dbh->do("drop table if exists $abort_table");
  68.   $dbh->do("create table $abort_table (id int(6) not null) type=heap") ||
  69.     die $DBI::errstr;
  70. }
  71.  
  72. $dbh->do("delete from $abort_table");
  73. $dbh->disconnect; $dbh=0;    # Close handler
  74. $|= 1;                # Autoflush
  75.  
  76. ####
  77. #### Start the tests
  78. ####
  79.  
  80. for ($i=0 ; $i < $opt_threads ; $i ++)
  81. {
  82.   test_insert() if (($pid=fork()) == 0); $work{$pid}="insert";
  83. }
  84. for ($i=0 ; $i < $numtables ; $i ++)
  85. {
  86.   test_insert($i,$i) if (($pid=fork()) == 0); $work{$pid}="insert_one";
  87. }
  88. for ($i=0 ; $i < $opt_threads ; $i ++)
  89. {
  90.   test_select() if (($pid=fork()) == 0); $work{$pid}="select_key";
  91. }
  92. test_join() if (($pid=fork()) == 0); $work{$pid}="test_join";
  93. test_select_count() if (($pid=fork()) == 0); $work{$pid}="select_count";
  94. test_delete() if (($pid=fork()) == 0); $work{$pid}="delete";
  95. test_update() if (($pid=fork()) == 0); $work{$pid}="update";
  96. test_flush() if (($pid=fork()) == 0); $work{$pid}= "flush";
  97. test_check() if (($pid=fork()) == 0); $work{$pid}="check";
  98. test_repair() if (($pid=fork()) == 0); $work{$pid}="repair";
  99. test_alter() if (($pid=fork()) == 0); $work{$pid}="alter";
  100. #test_database("test2") if (($pid=fork()) == 0); $work{$pid}="check_database";
  101.  
  102. print "Started " . ($opt_threads*2+4) . " threads\n";
  103.  
  104. $errors=0;
  105. $running_insert_threads=$opt_threads+$numtables;
  106. while (($pid=wait()) != -1)
  107. {
  108.   $ret=$?/256;
  109.   print "thread '" . $work{$pid} . "' finished with exit code $ret\n";
  110.   if ($work{$pid} =~ /^insert/)
  111.   {
  112.     if (!--$running_insert_threads)
  113.     {
  114.       # Time to stop other threads
  115.       signal_abort();
  116.     }
  117.   }
  118.   $errors++ if ($ret != 0);
  119. }
  120.  
  121. #
  122. # Cleanup
  123. #
  124.  
  125. if (!$opt_skip_delete && !$errors)
  126. {
  127.   my $table_def;
  128.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  129.               $opt_user, $opt_password,
  130.             { PrintError => 0}) || die $DBI::errstr;
  131.  
  132.   $dbh->do("drop table $abort_table");
  133.   foreach $table_def (@testtables)
  134.   {
  135.     $dbh->do("drop table " . $table_def->[0]);
  136.   }
  137.   $dbh->disconnect; $dbh=0;    # Close handler
  138. }
  139.  
  140. print ($errors ? "Test failed\n" :"Test ok\n");
  141. $end_time=new Benchmark;
  142. print "Total time: " .
  143.   timestr(timediff($end_time, $start_time),"noc") . "\n";
  144.  
  145. exit(0);
  146.  
  147.  
  148. #
  149. # Insert records in the table
  150. #
  151.  
  152. sub test_insert
  153. {
  154.   my ($from_table,$to_table)= @_;
  155.   my ($dbh,$i,$j,$count,$table_def,$table);
  156.  
  157.   if (!defined($from_table))
  158.   {
  159.     $from_table=0; $to_table=$numtables-1;
  160.   }
  161.  
  162.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  163.               $opt_user, $opt_password,
  164.             { PrintError => 0}) || die $DBI::errstr;
  165.  
  166.   for ($i=$count=0 ; $i < $opt_loop_count; $i++)
  167.   {
  168.     for ($j= $from_table ; $j <= $to_table ; $j++)
  169.     {
  170.       my ($table)= ($testtables[$j]->[0]);
  171.       $dbh->do("insert into $table values (NULL,'This is entry $i','',0)") || die "Got error on insert: $DBI::errstr\n";
  172.       $count++;
  173.     }
  174.   }
  175.   $dbh->disconnect; $dbh=0;
  176.   print "Test_insert: Inserted $count rows\n";
  177.   exit(0);
  178. }
  179.  
  180.  
  181. #
  182. # select records
  183. # Do continously select over all tables as long as there is changed
  184. # rows in the table
  185. #
  186.  
  187. sub test_select
  188. {
  189.   my ($dbh, $i, $j, $count, $loop);
  190.  
  191.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  192.               $opt_user, $opt_password,
  193.             { PrintError => 0}) || die $DBI::errstr;
  194.  
  195.   $count_query=make_count_query($numtables);
  196.   $count=0;
  197.   $loop=9999;
  198.  
  199.   $i=0;
  200.   while (($i++ % 100) || !test_if_abort($dbh))
  201.   {
  202.     if ($loop++ >= 100)
  203.     {
  204.       $loop=0;
  205.       $row_counts=simple_query($dbh, $count_query);
  206.     }
  207.     for ($j=0 ; $j < $numtables ; $j++)
  208.     {
  209.       my ($id)= int rand $row_counts->[$j];
  210.       my ($table)= $testtables[$j]->[0];
  211.       simple_query($dbh, "select id,info from $table where id=$id");
  212.       $count++;
  213.     }
  214.   }
  215.   $dbh->disconnect; $dbh=0;
  216.   print "Test_select: Executed $count selects\n";
  217.   exit(0);
  218. }
  219.  
  220. #
  221. # Do big select count(distinct..) over the table
  222.  
  223. sub test_select_count
  224. {
  225.   my ($dbh, $i, $j, $count, $loop);
  226.  
  227.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  228.               $opt_user, $opt_password,
  229.             { PrintError => 0}) || die $DBI::errstr;
  230.  
  231.   $count=0;
  232.   $i=0;
  233.   while (!test_if_abort($dbh))
  234.   {
  235.     for ($j=0 ; $j < $numtables ; $j++)
  236.     {
  237.       my ($table)= $testtables[$j]->[0];
  238.       simple_query($dbh, "select count(distinct marker),count(distinct id),count(distinct info) from $table");
  239.       $count++;
  240.     }
  241.     sleep(20);        # This query is quite slow
  242.   }
  243.   $dbh->disconnect; $dbh=0;
  244.   print "Test_select: Executed $count select count(distinct) queries\n";
  245.   exit(0);
  246. }
  247.  
  248. #
  249. # select records
  250. # Do continously joins between the first and second table
  251. #
  252.  
  253. sub test_join
  254. {
  255.   my ($dbh, $i, $j, $count, $loop);
  256.  
  257.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  258.               $opt_user, $opt_password,
  259.             { PrintError => 0}) || die $DBI::errstr;
  260.  
  261.   $count_query=make_count_query($numtables);
  262.   $count=0;
  263.   $loop=9999;
  264.  
  265.   $i=0;
  266.   while (($i++ % 100) || !test_if_abort($dbh))
  267.   {
  268.     if ($loop++ >= 100)
  269.     {
  270.       $loop=0;
  271.       $row_counts=simple_query($dbh, $count_query);
  272.     }
  273.     for ($j=0 ; $j < $numtables-1 ; $j++)
  274.     {
  275.       my ($id)= int rand $row_counts->[$j];
  276.       my ($t1,$t2)= ($testtables[$j]->[0],$testtables[$j+1]->[0]);
  277.       simple_query($dbh, "select $t1.id,$t2.info from $t1, $t2 where $t1.id=$t2.id and $t1.id=$id");
  278.       $count++;
  279.     }
  280.   }
  281.   $dbh->disconnect; $dbh=0;
  282.   print "Test_join: Executed $count joins\n";
  283.   exit(0);
  284. }
  285.  
  286. #
  287. # Delete 1-5 rows from the first 2 tables.
  288. # Test ends when the number of rows for table 3 didn't change during
  289. # one loop
  290. #
  291.  
  292. sub test_delete
  293. {
  294.   my ($dbh, $i,$j, $row_counts, $count_query, $table_count, $count);
  295.  
  296.   $table_count=2;
  297.   $count=0;
  298.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  299.               $opt_user, $opt_password,
  300.             { PrintError => 0}) || die $DBI::errstr;
  301.  
  302.   $count_query=make_count_query($table_count+1);
  303.  
  304.   sleep(5);            # Give time to insert some rows
  305.   $i=0;
  306.   while (($i++ % 10) || !test_if_abort($dbh))
  307.   {
  308.     sleep(1);
  309.     $row_counts=simple_query($dbh, $count_query);
  310.  
  311.     for ($j=0 ; $j < $table_count ; $j++)
  312.     {
  313.       my ($id)= int rand $row_counts->[$j];
  314.       my ($table)= $testtables[$j]->[0];
  315.       $dbh->do("delete from $table where id >= $id-2 and id <= $id +2") || die "Got error on delete from $table: $DBI::errstr\n";
  316.       $count++;
  317.     }
  318.   }
  319.   $dbh->disconnect; $dbh=0;
  320.   print "Test_delete: Executed $count deletes\n";
  321.   exit(0);
  322. }
  323.  
  324. #
  325. # Update the flag for table 2 and 3
  326. # Will abort after a while when table1 doesn't change max value
  327. #
  328.  
  329. sub test_update
  330. {
  331.   my ($dbh, $i, $j, $row_counts, $count_query, $count, $loop);
  332.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  333.               $opt_user, $opt_password,
  334.             { PrintError => 0}) || die $DBI::errstr;
  335.  
  336.   $count_query=make_count_query(3);
  337.   $loop=9999;
  338.   $count=0;
  339.  
  340.   sleep(5);            # Give time to insert some rows
  341.   $i=0;
  342.   while (($i++ % 100) || !test_if_abort($dbh))
  343.   {
  344.     if ($loop++ >= 100)
  345.     {
  346.       $loop=0;
  347.       $row_counts=simple_query($dbh, $count_query);
  348.     }
  349.  
  350.     for ($j=1 ; $j <= 2 ; $j++)
  351.     {
  352.       my ($id)= int rand $row_counts->[$j];
  353.       my ($table)= $testtables[$j]->[0];
  354.       # Fix to not change the same rows as the above delete
  355.       $id= ($id + $count) % $row_counts->[$j];
  356.  
  357.       $dbh->do("update $table set flag=flag+1 where id >= $id-2 and id <= $id +2") || die "Got error on update of $table: $DBI::errstr\n";
  358.       $count++;
  359.     }
  360.   }
  361.   $dbh->disconnect; $dbh=0;
  362.   print "Test_update: Executed $count updates\n";
  363.   exit(0);
  364. }
  365.  
  366.  
  367. #
  368. # Run a check on all tables except the last one
  369. # (The last one is not checked to put pressure on the key cache)
  370. #
  371.  
  372. sub test_check
  373. {
  374.   my ($dbh, $row, $i, $j, $type, $table);
  375.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  376.               $opt_user, $opt_password,
  377.             { PrintError => 0}) || die $DBI::errstr;
  378.  
  379.   $type= "check";
  380.   for ($i=$j=0 ; !test_if_abort($dbh) ; $i++)
  381.   {
  382.     sleep(1000);
  383.     $table=$testtables[$j]->[0];
  384.     $sth=$dbh->prepare("$type table $table") || die "Got error on prepare: $DBI::errstr\n";
  385.     $sth->execute || die $DBI::errstr;
  386.  
  387.     while (($row=$sth->fetchrow_arrayref))
  388.     {
  389.       if ($row->[3] ne "OK")
  390.       {
  391.     print "Got error " . $row->[3] . " when doing $type on $table\n";
  392.     exit(1);
  393.       }
  394.     }
  395.     if (++$j == $numtables-1)
  396.     {
  397.       $j=0;
  398.     }
  399.   }
  400.   $dbh->disconnect; $dbh=0;
  401.   print "test_check: Executed $i checks\n";
  402.   exit(0);
  403. }
  404.  
  405. #
  406. # Do a repair on the first table once in a while
  407. #
  408.  
  409. sub test_repair
  410. {
  411.   my ($dbh, $row, $i, $type, $table);
  412.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  413.               $opt_user, $opt_password,
  414.             { PrintError => 0}) || die $DBI::errstr;
  415.  
  416.   $type= "repair";
  417.   for ($i=0 ; !test_if_abort($dbh) ; $i++)
  418.   {
  419.     sleep(2000);
  420.     $table=$testtables[0]->[0];
  421.     $sth=$dbh->prepare("$type table $table") || die "Got error on prepare: $DBI::errstr\n";
  422.     $sth->execute || die $DBI::errstr;
  423.  
  424.     while (($row=$sth->fetchrow_arrayref))
  425.     {
  426.       if ($row->[3] ne "OK")
  427.       {
  428.     print "Got error " . $row->[3] . " when doing $type on $table\n";
  429.     exit(1);
  430.       }
  431.     }
  432.   }
  433.   $dbh->disconnect; $dbh=0;
  434.   print "test_repair: Executed $i repairs\n";
  435.   exit(0);
  436. }
  437.  
  438. #
  439. # Do a flush tables on table 3 and 4 once in a while
  440. #
  441.  
  442. sub test_flush
  443. {
  444.   my ($dbh,$count,$tables);
  445.  
  446.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  447.               $opt_user, $opt_password,
  448.             { PrintError => 0}) || die $DBI::errstr;
  449.  
  450.   $tables=$testtables[2]->[0] . "," . $testtables[3]->[0];
  451.  
  452.   $count=0;
  453.   while (!test_if_abort($dbh))
  454.   {
  455.     sleep(3000);
  456.     $dbh->do("flush tables $tables") ||
  457.       die "Got error on flush $DBI::errstr\n";
  458.     $count++;
  459.   }
  460.   $dbh->disconnect; $dbh=0;
  461.   print "flush: Executed $count flushs\n";
  462.   exit(0);
  463. }
  464.  
  465.  
  466. #
  467. # Test all tables in a database
  468. #
  469.  
  470. sub test_database
  471. {
  472.   my ($database) = @_;
  473.   my ($dbh, $row, $i, $type, $tables);
  474.   $dbh = DBI->connect("DBI:mysql:$database:$opt_host",
  475.               $opt_user, $opt_password,
  476.             { PrintError => 0}) || die $DBI::errstr;
  477.  
  478.   $tables= join(',',$dbh->func('_ListTables'));
  479.   $type= "check";
  480.   for ($i=0 ; !test_if_abort($dbh) ; $i++)
  481.   {
  482.     sleep(120);
  483.     $sth=$dbh->prepare("$type table $tables") || die "Got error on prepare: $DBI::errstr\n";
  484.     $sth->execute || die $DBI::errstr;
  485.  
  486.     while (($row=$sth->fetchrow_arrayref))
  487.     {
  488.       if ($row->[3] ne "OK")
  489.       {
  490.     print "Got error " . $row->[2] . " " . $row->[3] . " when doing $type on " . $row->[0] . "\n";
  491.     exit(1);
  492.       }
  493.     }
  494.   }
  495.   $dbh->disconnect; $dbh=0;
  496.   print "test_check: Executed $i checks\n";
  497.   exit(0);
  498. }
  499.  
  500. #
  501. # Test ALTER TABLE on the second table
  502. #
  503.  
  504. sub test_alter
  505. {
  506.   my ($dbh, $row, $i, $type, $table);
  507.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  508.               $opt_user, $opt_password,
  509.             { PrintError => 0}) || die $DBI::errstr;
  510.  
  511.   for ($i=0 ; !test_if_abort($dbh) ; $i++)
  512.   {
  513.     sleep(100);
  514.     $table=$testtables[1]->[0];
  515.     $sth=$dbh->prepare("ALTER table $table modify info char(32)") || die "Got error on prepare: $DBI::errstr\n";
  516.     $sth->execute || die $DBI::errstr;
  517.   }
  518.   $dbh->disconnect; $dbh=0;
  519.   print "test_alter: Executed $i ALTER TABLE\n";
  520.   exit(0);
  521. }
  522.  
  523.  
  524. #
  525. # Help functions
  526. #
  527.  
  528. sub signal_abort
  529. {
  530.   my ($dbh);
  531.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  532.               $opt_user, $opt_password,
  533.             { PrintError => 0}) || die $DBI::errstr;
  534.  
  535.   $dbh->do("insert into $abort_table values(1)") || die $DBI::errstr;
  536.   $dbh->disconnect; $dbh=0;
  537.   exit(0);
  538. }
  539.  
  540.  
  541. sub test_if_abort()
  542. {
  543.   my ($dbh)=@_;
  544.   $row=simple_query($dbh,"select * from $opt_db.$abort_table");
  545.   return (defined($row) && defined($row->[0]) != 0) ? 1 : 0;
  546. }
  547.  
  548.  
  549. sub make_count_query
  550. {
  551.   my ($table_count)= @_;
  552.   my ($tables, $count_query, $i, $tables_def);
  553.   $tables="";
  554.   $count_query="select high_priority ";
  555.   $table_count--;
  556.   for ($i=0 ; $i < $table_count ; $i++)
  557.   {
  558.     my ($table_def)= $testtables[$i];
  559.     $tables.=$table_def->[0] . ",";
  560.     $count_query.= "max(" . $table_def->[0] . ".id),";
  561.   }
  562.   $table_def=$testtables[$table_count];
  563.   $tables.=$table_def->[0];
  564.   $count_query.= "max(" . $table_def->[0] . ".id) from $tables";
  565.   return $count_query;
  566. }
  567.  
  568. sub simple_query()
  569. {
  570.   my ($dbh, $query)= @_;
  571.   my ($sth,$row);
  572.  
  573.   $sth=$dbh->prepare($query) || die "Got error on '$query': " . $dbh->errstr . "\n";
  574.   $sth->execute || die "Got error on '$query': " . $dbh->errstr . "\n";
  575.   $row= $sth->fetchrow_arrayref();
  576.   $sth=0;
  577.   return $row;
  578. }
  579.